<?php

class IndexAction extends CommonAction
{
    /**
    +----------------------------------------------------------
    * 默认操作
    +----------------------------------------------------------
    */
    public function index()
    {
		$this->menu();
		$this->display();

    }

    /*菜单树*/
	public function tree()
	{
		$tree = array();
		$tree = $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];



		$method = $_POST;
		if($method['p'] == 'menu')
		{
			$menutree = array();

			foreach($tree as $v)
			{
				if($v['leaf'] ==false ) $menutree[] = $v;
			}
			echo(json_encode($menutree));
		}
		else
		{
			$subtree = array();
			foreach($tree as $v)
			{
				if($v[pid] == $method['p'])	$subtree[] = $v;
			}
			echo(json_encode($subtree));
		}

	}
	// 菜单页面
	private function menu() {

        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
            //显示菜单项

            $menu  = array();
            if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])) {

                //如果已经缓存，直接读取缓存
                $menu   =   $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];

            }else {
                //读取数据库模块列表生成菜单项
                $node    =   M("Node");
				$id	=	$node->getField("id");
				//print_r($id);
				$where['level']=2;
				$where['status']=1;
				$where['pid']=$id;
                $list	=	$node->where($where)->field('id,name,group_id,title')->order('sort asc')->select();

                $accessList = $_SESSION['_ACCESS_LIST'];
				$model	=	M("Group");
				$listmenu	=	$model->where('status=1')->Field('id,title')->select();
				//
				$vv = array();

				foreach($listmenu as $v)
				{
						$vv['id'] = 'g_'. $v['id'];
						$vv['text'] = $v['title'];
						$m[$v['id']] = $vv;
				}
				//print_r($m);

                foreach($list as $key=>$module) {

                     if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
						 if($m[$module['group_id']]){
							 $menu[ 'g_'. $module['group_id']] = $m[$module['group_id']];
						 }
                        //设置模块访问权限
                        $module['access'] =   1;
						$module['pid'] = 'g_' . $module['group_id'];
						$module['text'] = $module['title'];
						$module['url'] = $module['name'];
						$module['leaf'] = true;
                        $menu[$key]  = $module;
                    }
                }
				//print_r($menu);
                //缓存菜单访问
                $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]	=	$menu;
            }

		}

	}
	public function main()
	{
		$this->display();
	}

}
?>